package com.cqrcb.cloud.service.quota.thread;

import com.cqrcb.cloud.config.log.LogAspect;
import com.cqrcb.cloud.entity.api.thread.CustomThread;
import com.cqrcb.cloud.entity.quota.MultiQuotaCtrlMarketMain;
import com.cqrcb.cloud.entity.quota.io.MultiQuotaCtrlMarketMainIO;
import com.cqrcb.cloud.service.quota.MultiQuotaCtrlMarketMainService;
import com.cqrcb.cloud.service.quota.base.QuotaBaseServiceImpl;
import org.slf4j.MDC;

/**
 * @author ：yangxueyong
 * @date ：Created in 2021/6/1 20:55
 */
public class DeductionQuotaThread extends CustomThread {
    private MultiQuotaCtrlMarketMainIO record;
    private MultiQuotaCtrlMarketMainService multiQuotaCtrlMarketMainService;
    private QuotaBaseServiceImpl quotaBaseService;
    /**
     * 跟踪id
     * 由于service层会销毁traceId，因此在调用之前应该记录当下的traceId
     */
    private String traceId;


    public DeductionQuotaThread(MultiQuotaCtrlMarketMainIO record,
                                QuotaBaseServiceImpl quotaBaseService,
                                MultiQuotaCtrlMarketMainService multiQuotaCtrlMarketMainService
                                ){
        super(record.getType(),record.getDataType(),record.getDataId(),record.getOperationType());
        this.record = record;
        this.multiQuotaCtrlMarketMainService = multiQuotaCtrlMarketMainService;
        this.quotaBaseService = quotaBaseService;
        this.traceId = MDC.get(LogAspect.TRACE_ID);
    }
    @Override
    public void run() {
        MDC.put(LogAspect.TRACE_ID, traceId);
        MultiQuotaCtrlMarketMain multiQuotaCtrlMarketMain = (MultiQuotaCtrlMarketMain) quotaBaseService.getMultiQuotaCtrlMarketMainForRedis(record);
        multiQuotaCtrlMarketMainService.updateByPrimaryKeySelective(multiQuotaCtrlMarketMain);
    }
}
